<?php

namespace App\Http\Controllers;

use App\Models\Category;
use App\Models\Course;
use App\Util\Helper;

/**
 *
 * Author: TheSmile
 * Date: 2023/5/19 14:29
 * Email: <594652523@qq.com>
 */
class IndexController
{

    public function index()
    {
        return view('index');
    }

    public function course()
    {
        $list = Course::where('status', 1)->get(['id', 'name', 'category_id'])->toArray();
        $tree = [];
        if (!empty($list)) {
            $category = Category::all(['id', 'pid as parent_id', 'name'])->toArray();
            $tree     = Helper::getTree($category);
            foreach ($list as $item) {
                $arr[$item['category_id']][] = $item;
            }
            foreach ($tree as &$item) {
                foreach ($item['children'] as &$value) {
                    foreach ($arr as $key => $val) {
                        if ($key == $value['id']) {
                            $value['children'] = $val;
                        }
                    }
                }
                unset($value);
            }
            unset($item);

        }
//        dd($tree);
        return view('course', ['tree' => $tree]);
    }

    public function login()
    {
        return view('login');
    }

    public function register()
    {
        return view('register');
    }

    public function courseDetail()
    {
        $courseId    = request('sid') ?? '';
        $courseInfo  = Course::find($courseId);
        $relatedList = Course::where('id', '!=', $courseId)->where('status', 1)->get()->toArray();
        return view('detail', compact("courseInfo", "relatedList"));
    }
}

